package com.terios;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.terios.http.JsonAnalysis;
import com.weibo.net.WeiboException;

/**
 * User.java 2012-10-1 下午7:49:10
 * 
 * @author Wang Tengrui
 */
public class User extends JsonAnalysis
{

	private long id; // 用户ID
	private String screenName; // 微博昵称
	private String name; // 友好显示名称，如Bill Gates(此特性暂不支持)
	private int province; // 省份编码（参考省份编码表）
	private int city; // 城市编码（参考城市编码表）
	private String location; // 地址
	private String description; // 个人描述
	private String url; // 用户博客地址
	private String profileImageUrl; // 自定义图像
	private String userDomain; // 用户个性化URL
	private String gender; // 性别,m--男，f--女,n--未知
	private int followersCount; // 粉丝数
	private int friendsCount; // 关注数
	private int statusesCount; // 微博数
	private int favouritesCount; // 收藏数
	private Date createdAt; // 创建时间
	private boolean following; // 保留字段,是否已关注(此特性暂不支持)
	private boolean verified; // 加V标示，是否微博认证用户
	private boolean geoEnabled; // 地理
	private boolean allowAllActMsg; // 保留字段（暂时不支持）

	private Status status;

	public User(JSONObject userJsonObject) throws WeiboException
	{
		super();
		init(userJsonObject);
	}
	
	public User(String responseString) throws WeiboException{
		super();
		JSONObject userJsonObject = getJSONObject(responseString);
		init(userJsonObject);
	}

	private void init(JSONObject userJsonObject) throws WeiboException
	{
		try
		{
			id = getLong("id", userJsonObject);
			screenName = userJsonObject.getString("screen_name");
			name = userJsonObject.getString("name");
			province = getInt("province", userJsonObject);
			city = getInt("city", userJsonObject);
			location = userJsonObject.getString("location");
			description = userJsonObject.getString("description");
			profileImageUrl = userJsonObject.getString("profile_image_url");
			url = userJsonObject.getString("url");
			allowAllActMsg = getBoolean("allow_all_act_msg", userJsonObject);
			followersCount = getInt("followers_count", userJsonObject);
			friendsCount = getInt("friends_count", userJsonObject);
			createdAt = parseDate(userJsonObject.getString("created_at"),
					"EEE MMM dd HH:mm:ss z yyyy");
			favouritesCount = getInt("favourites_count", userJsonObject);
			following = getBoolean("following", userJsonObject);
			verified = getBoolean("verified", userJsonObject);
			statusesCount = getInt("statuses_count", userJsonObject);
			userDomain = userJsonObject.getString("domain");
			gender = userJsonObject.getString("gender");

			if (!userJsonObject.isNull("status"))
			{
				setStatus(new Status(userJsonObject.getJSONObject("status")));
			}
		} catch (JSONException e)
		{
			throw new WeiboException(e.getMessage() + ":"
					+ userJsonObject.toString(), e);
		}
	}

	/**
	 * 返回用户自定义头像URL对象
	 * 
	 * @return the profile image url of the user
	 */
	/*public URL getProfileImageURL()
	{
		try
		{
			return new URL(profileImageUrl);
		} catch (MalformedURLException ex)
		{
			return null;
		}
	}*/

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public String getScreenName()
	{
		return screenName;
	}

	public void setScreenName(String screenName)
	{
		this.screenName = screenName;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getProvince()
	{
		return province;
	}

	public void setProvince(int province)
	{
		this.province = province;
	}

	public int getCity()
	{
		return city;
	}

	public void setCity(int city)
	{
		this.city = city;
	}

	public String getLocation()
	{
		return location;
	}

	public void setLocation(String location)
	{
		this.location = location;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

	public String getProfileImageUrl()
	{
		return profileImageUrl;
	}

	public void setProfileImageUrl(String profileImageUrl)
	{
		this.profileImageUrl = profileImageUrl;
	}

	public String getUserDomain()
	{
		return userDomain;
	}

	public void setUserDomain(String userDomain)
	{
		this.userDomain = userDomain;
	}

	public String getGender()
	{
		return gender;
	}

	public void setGender(String gender)
	{
		this.gender = gender;
	}

	public int getFollowersCount()
	{
		return followersCount;
	}

	public void setFollowersCount(int followersCount)
	{
		this.followersCount = followersCount;
	}

	public int getFriendsCount()
	{
		return friendsCount;
	}

	public void setFriendsCount(int friendsCount)
	{
		this.friendsCount = friendsCount;
	}

	public int getStatusesCount()
	{
		return statusesCount;
	}

	public void setStatusesCount(int statusesCount)
	{
		this.statusesCount = statusesCount;
	}

	public int getFavouritesCount()
	{
		return favouritesCount;
	}

	public void setFavouritesCount(int favouritesCount)
	{
		this.favouritesCount = favouritesCount;
	}

	public Date getCreatedAt()
	{
		return createdAt;
	}

	public void setCreatedAt(Date createdAt)
	{
		this.createdAt = createdAt;
	}

	public boolean isFollowing()
	{
		return following;
	}

	public void setFollowing(boolean following)
	{
		this.following = following;
	}

	public boolean isVerified()
	{
		return verified;
	}

	public void setVerified(boolean verified)
	{
		this.verified = verified;
	}

	public boolean isGeoEnabled()
	{
		return geoEnabled;
	}

	public void setGeoEnabled(boolean geoEnabled)
	{
		this.geoEnabled = geoEnabled;
	}

	public boolean isAllowAllActMsg()
	{
		return allowAllActMsg;
	}

	public void setAllowAllActMsg(boolean allowAllActMsg)
	{
		this.allowAllActMsg = allowAllActMsg;
	}

	public void setStatus(Status status)
	{
		this.status = status;
	}

	public Status getStatus()
	{
		return status;
	}

	@Override
	public String toString()
	{
		// TODO Auto-generated method stub
		return "请重写User类的toString方法";
	}

}
